package com.xingkeduo.controller;

import com.xingkeduo.model.param.StoreStaffParam;
import com.xingkeduo.model.param.UnbindStoreStaff;
import com.xingkeduo.model.po.TStoreStaff;
import com.xingkeduo.model.vo.StaffAttendanceSummaryVo;
import com.xingkeduo.model.vo.StoreStaffVo;
import com.xingkeduo.service.StaffAttendanceService;
import com.xingkeduo.service.StoreStaffService;
import com.xingkeduo.util.TimeUtil;
import com.xingkeduo.util.tool.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: 中文输入法发挥不稳定的刘川
 * @Date: 19-3-13 上午11:40
 */
@RestController
@RequestMapping("/store/stuff")
public class StoreStaffController {

    @Autowired
    StoreStaffService storeStaffService;
    @Autowired
    StaffAttendanceService staffAttendanceService;

    /**
     * @return java.lang.String
     * @description 通过员工id和姓名 绑定到店铺
     * @author 中文输入法发挥不稳定的刘川
     * @date 19-3-15 下午7:36
     */
    @PostMapping("/bingStaffToStoreForWeek")
    public ResponseMessage bingStaffToStoreForWeek(@RequestBody StoreStaffParam storeStaffParam) {
        if(storeStaffParam.getSelectDate() == null){
            return ResponseMessage.error("缺少新增时间", 500);
        }
        if(StringUtils.isEmpty(storeStaffParam.getStaffId())){
            return ResponseMessage.error("缺少发型师id", 500);
        }
        if(StringUtils.isEmpty(storeStaffParam.getStoreId())){
            return ResponseMessage.error("缺少店铺id", 500);
        }
        if(StringUtils.isEmpty(storeStaffParam.getStoreName())){
            return ResponseMessage.error("缺少店铺name", 500);
        }
        if(StringUtils.isEmpty(storeStaffParam.getManagerId())){
            return ResponseMessage.error("缺少操作人id", 500);
        }
        if(StringUtils.isEmpty(storeStaffParam.getStaffName())){
            return ResponseMessage.error("缺少发型师name", 500);
        }
        Date selectWeekMondayDate = TimeUtil.getDateOfMonday(storeStaffParam.getSelectDate(), 0);

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        Calendar calendar= Calendar.getInstance();

        Date thisWeekMondayDate = TimeUtil.getDateOfMonday(format.format(calendar.getTime()), 0);
        //判断传入时间当前周的周一 是否在当前周的周一之前
        if(selectWeekMondayDate.before(thisWeekMondayDate)){
            return ResponseMessage.error("新增时间不能是历史时间",500);
        }
        TStoreStaff storeStaff = storeStaffService.getStaffToStoreForWeek(storeStaffParam.getSelectDate(), storeStaffParam.getStaffId(), storeStaffParam.getStoreId());
        if(storeStaff != null){
            return ResponseMessage.error("新增发型师，在当前周已经绑定了该店铺，请勿重复添加",500);
        }
        boolean bindFlag = storeStaffService.bingStaffToStoreForWeek(
                storeStaffParam.getStaffId(),
                storeStaffParam.getStaffName(),
                storeStaffParam.getStoreId(),
                storeStaffParam.getStoreName(),
                storeStaffParam.getManagerId(),
                storeStaffParam.getSelectDate());
        SimpleDateFormat simdf = new SimpleDateFormat("yyyy-MM-dd");
        Map<Long, StaffAttendanceSummaryVo> voMap = staffAttendanceService.getStaffAttendance(simdf.format(storeStaffParam.getSelectDate()), storeStaffParam.getStoreId(), storeStaffParam.getStaffId());
        return bindFlag ? ResponseMessage.ok(voMap.values(),"新增成功") : ResponseMessage.error("新增失败", 500);
    }


    /**
     * @description  根据店铺ID获取店铺发型师 TODO 接口待根据业务完善
     * @Author 情绪极其不稳定的Shadow
     * @Param [storeId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @GetMapping("/getStoreStaffList")
    public ResponseMessage getStoreStaffList(Long storeId){
        if(null == storeId){
            return ResponseMessage.error("店铺ID不能为空",500);
        }
        return ResponseMessage.ok(storeStaffService.getStoreStaffList(storeId));
    }


    /**
     * @description  解除当周绑定关系人员
     * @Author 情绪极其不稳定的Shadow
     * @Param [bindId, managerId]
     * @return com.xingkeduo.util.tool.ResponseMessage
     **/
    @PostMapping("/unbindStoreStaff")
    public ResponseMessage unbindStoreStaff(@RequestBody UnbindStoreStaff unbindStoreStaff) {
        if(null == unbindStoreStaff){
            return ResponseMessage.error("参数不能为空", 500);
        }
        if (null == unbindStoreStaff.getManagerId()) {
            return ResponseMessage.error("未能获取到操作人，操作失败", 500);
        }
        if (null == unbindStoreStaff.getBindId() || unbindStoreStaff.getBindId().size() == 0) {
            return ResponseMessage.error("确保数据完整性", 500);
        }

        ResponseMessage responseMessage = storeStaffService.unbindStoreStaff(unbindStoreStaff.getBindId(), unbindStoreStaff.getManagerId());
        return ResponseMessage.ok(responseMessage);
    }

    @GetMapping("/getbindStoreStaffByDate")
    public ResponseMessage getbindStoreStaffByDate(Long storeId, String selectDate){
        if(StringUtils.isEmpty(storeId)){
            return ResponseMessage.error("缺少店铺Id", 500);
        }
        if(StringUtils.isEmpty(selectDate)){
            return ResponseMessage.error("缺少当前时间", 500);
        }
        List<StoreStaffVo> storeStaffVos = storeStaffService.getbindStoreStaffByDate(storeId, selectDate);
        return ResponseMessage.ok(storeStaffVos);
    }

}
